package com.db.client.data;

import java.util.LinkedList;
import java.util.List;

import com.db.protocol.tags.OrderStatus;

public class OrderTable {
	
	private List<ClientOrder> orders;
	
	private List<Execution> executions;
	
	private DataListener listener;
	
	public void setListener(DataListener listener) {
		this.listener = listener;
	}
	
	public OrderTable() {
		orders = new LinkedList<ClientOrder>();
		executions = new LinkedList<Execution>();
	}
	
	public void addOrder(ClientOrder order) {
		synchronized (orders) {
			orders.add(order);
		}		
		if (listener != null) {
			listener.addOrder(order);
		}
	}
	
	public void setStatus(String orderID, OrderStatus status, String text) {
		for (ClientOrder order : orders) {
			if (order.getOrderID().equals(orderID)) {
				synchronized (order) {
					order.setStatus(status, text);
				}		
				if (listener != null) {
					listener.changeStatusOrder(order);
				}
				break;
			}
		}
	}

	public void setStatus(String orderID, OrderStatus status) {
		for (ClientOrder order : orders) {
			if (order.getOrderID().equals(orderID)) {
				synchronized (order) {
					order.setStatus(status);
				}		
				if (listener != null) {
					listener.changeStatusOrder(order);
				}
				return;
			}
		}
	}
	
	public void addExecution(Execution execution) {
		synchronized (executions) {
			executions.add(execution);
		}
		if (listener != null) {
			listener.addExecution(execution);	
		}
	}
}
